import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

public class ContentSelector extends JPanel{

    protected JTextArea bottomLeftContent, bottomMiddleContent, bottomRightContent;
    protected JTextArea topLeftContent, topMiddleContent, topRightContent;
    protected JTextArea[] content;
    private JScrollPane topMiddleScroll;
    private JLabel albumArt;
    private Lyrics lyricFinder;
    private JButton getImage;
    private JPanel artScroll;
    private LastFMContentGrabber grabber0, grabber1,grabber2,grabber3;
    private String currentArtist, currentSong,currentAlbum;
    public ContentSelector(MusicSelector musicSelector){
        setBackground(Color.WHITE);
        setVisible(true);
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        
        content = new JTextArea[6];
        content[0] = textSetup("Song Lyrics");
        content[0].setLineWrap(true);

        //topLeftContent = textSetup("Song Lyrics");
        //topLeftContent.setLineWrap(true);
        JScrollPane topLeftScroll = scrollSetup(content[0]);

        content[1] = textSetup("Artist Biography");
        content[1].setWrapStyleWord(true);
        content[1].setLineWrap(true);
        //topMiddleContent = textSetup("Artist Biography");
        //topMiddleContent.setWrapStyleWord(true);
        //topMiddleContent.setLineWrap(true);
         

        JScrollPane topRightScroll = scrollSetup(content[1]);

        content[2] = textSetup("Shouts");
        content[2].setWrapStyleWord(true);
        content[2].setLineWrap(true);
        //bottomLeftContent = textSetup("Shouts");
        //bottomLeftContent.setLineWrap(true);
        JScrollPane bottomLeftScroll = scrollSetup(content[2]);

        content[3] = textSetup("Album Info");
        content[3].setWrapStyleWord(true);
        content[3].setLineWrap(true);
        JScrollPane bottomMiddleScroll = scrollSetup(content[3]);


        bottomRightContent = textSetup("");
        JScrollPane bottomRightScroll = scrollSetup(bottomRightContent);

        //Album art

        albumArt = new JLabel();
        artScroll = new JPanel();
        artScroll.setBackground(Color.WHITE);
        artScroll.setLayout(new GridBagLayout());
        c.fill=GridBagConstraints.BOTH;
        artScroll.add(albumArt,c);
        JScrollPane topMiddleScroll = new JScrollPane();
        topMiddleScroll.setViewportView(artScroll);

       
        
        c.insets=new Insets( 5, 5, 5, 5 );
        c.weightx=1;
        c.weighty=1;
        c.anchor=GridBagConstraints.NORTHWEST;
        c.gridx=0;
        c.gridy=0;
        add(topLeftScroll,c);
        c.anchor=GridBagConstraints.NORTH;
        c.gridx++;
        add(topMiddleScroll, c);
        c.gridx++;
        c.anchor=GridBagConstraints.NORTHEAST;
        add(topRightScroll, c);
        c.gridy++;
        c.gridx=0;
        c.anchor=GridBagConstraints.SOUTHWEST;
        add(bottomLeftScroll,c);
        c.gridx++;
        c.anchor=GridBagConstraints.SOUTH;
        add(bottomMiddleScroll,c);
        c.gridx++;
        c.anchor=GridBagConstraints.SOUTHEAST;
        add(bottomRightScroll,c);

    }


    //Create JTextArea in default content size
    JTextArea textSetup(String content) {
        JTextArea text = new JTextArea(13, 37);
        text.setBackground(Color.WHITE);
        text.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
        text.setEditable(false);
        text.setText(content);
        return text;
    }

    //Create JScrollPane for content
    JScrollPane scrollSetup(JTextArea text) {
        JScrollPane scrollPane = new JScrollPane(text);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        return scrollPane;
    }

    public void clearContent(){
        //topLeftContent.setText("");
        //topMiddleContent.setText("");
        //bottomLeftContent.setText("");
        content[0].setText("Lyrics");
        content[1].setText("Artist Information");
        content[2].setText("Shouts");
        content[3].setText("Album information");
        topMiddleScroll.setViewportView(new JTextArea("Album"));
        
    }

    public void updateContent(String Artist, String Song, String Album){
        //display song information
        /*
        getImage.setVisible(true);
        artScroll.setVisible(false);
         */
        //search for lyrics

        currentArtist=Artist;
        currentSong=Song;
        currentAlbum=Album;
        lyricFinder = new Lyrics(currentSong, currentArtist, this, 0);
        try {
            new Thread(lyricFinder).start(); //getLyrics(content[0]);
        } catch (Exception e) {
            System.err.println("error with getting lyrics");
            e.printStackTrace();
        }
        content[0].setCaretPosition(0);
        
        grabber0 = new LastFMContentGrabber(this, currentArtist, currentSong,currentAlbum, 0, 2);
        grabber1 = new LastFMContentGrabber(this, currentArtist, currentSong,currentAlbum, 1, 1);
        grabber2 = new LastFMContentGrabber(this,currentArtist, currentSong,currentAlbum,3,0);
        grabber3 = new LastFMContentGrabber(this,currentArtist,currentSong,currentAlbum,2,3);

        new Thread(grabber0).start();
        new Thread(grabber1).start();
        new Thread(grabber2).start();
        new Thread(grabber3).start();
        //get artist info
        //topMiddleContent.setText(grabber.getArtistInfoString(currentArtist));
        content[1].setCaretPosition(0);

        //get shouts
        //bottomLeftContent.setText(grabber.getShoutsString(currentArtist, currentSong));
        content[3].setCaretPosition(0);

    }


    public Image getScaledImage(Image srcImg, int w, int h){
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }

    public static Image toImage(BufferedImage bufferedImage) {
        return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
    }

    public void updateAlbum(BufferedImage currentAlbum){
        ImageIcon art = new ImageIcon(toImage(currentAlbum));
        Image scaledImage = getScaledImage(art.getImage(),300, 300);
        ImageIcon newArt = new ImageIcon(scaledImage);
        albumArt.setIcon(newArt);
        
        repaint(); 
    }
}